//index.js

var app = getApp()
Page({
  data: {
    users: [],
    users_length: 0,

    approves: [],
    approves_length: 0,
    jsonresult: '',
    sourceUrl: 'https://eis.natappvip.cc/ITSupport',
    user: {},
    navbar: ['贡献排名', '我的贡献'],
    currentTab: 0  
  },
  

  onLoad: function (options) {
    this.setData({
      user: app.globalData.user
    })

    this.getUsers();
    this.getMyApproves();
  },

  onShow:function(){
    this.onLoad()
  },

  getUsers: function () {
    var that = this
    wx.request({
      url: 'https://eis.natappvip.cc/ITSupport/user_getAllUserOrderByContribute.action',
      data: {},
      header: {
        'Content-Type': 'application/json'
      },
      success: function (res) {
        let jsonresult = JSON.parse(res.data)
        console.log(jsonresult.result)
        if (jsonresult.result == "error") {
          that.setData({
            users: [],
            users_length: 0
          })
        } else {
          that.setData({
            users: jsonresult.users,
            users_length: jsonresult.users.length
          })
        }
      },
      fail: function (res) {
        console.log("failed:" & res)
      }
    })

  },
  navbarTap: function (e) {
    this.setData({
      currentTab: e.currentTarget.dataset.idx
    })
  },

  getMyApproves: function () {
    var that = this
    wx.request({
      url: 'https://eis.natappvip.cc/ITSupport/approve_getAllApproveByUser.action',
      data: {
        'user.userId': this.data.user.userId,
      },
      header: {
        'Content-Type': 'application/json'
      },
      success: function (res) {
        let jsonresult = JSON.parse(res.data)
        console.log(jsonresult.result)
        if (jsonresult.result == "error") {
          that.setData({
            approves: [],
            approves_length: 0
          })
        } else {
          that.setData({
            approves: jsonresult.approves,
            approves_length: jsonresult.approves.length
          })
        }
      },
      fail: function (res) {
        console.log("failed:" & res)
      }
    })

  },

  contribute:function(){
    wx.navigateTo({
      url: '../contribute/contribute'
    })
  }

})
